<?php
$mysql_hostname = "localhost";
$mysql_user = "root";
$mysql_password = "123456";
$mysql_database = "vnpt-qavah";
$bd = mysql_connect ( $mysql_hostname, $mysql_user, $mysql_password ) or die ( "Opps some thing went wrong" );
mysql_select_db ( $mysql_database, $bd ) or die ( "Opps some thing went wrong" );
$i = 0;
$id=$_POST['id'];
// $child=array();
// $result = array();
mysql_query ( 'SET CHARACTER SET utf8' );
$r = array ();
// $rs = mysql_query("select * from vnpt-qavah where parent_id=$id");
if(isset($id) && $id!='#'){
	$rs = mysql_query ( "select ug.group_id, ug.user_id, u.username from users_groups as ug, users as u where ug.user_id=u.id and ug.group_id=$id" );

	while ( $row = mysql_fetch_array ( $rs ) ) {
	$node ['id'] = "u".$row ['user_id'];
	$node ['parent'] = $row ['group_id'];
	$node ['type'] = "user";
	$node ['text'] = $row ['username'];

	array_push ( $r, $node );
	}
}else{
	$rs = mysql_query ( "select ug.group_id, g.name from users_groups as ug, groups as g where ug.group_id=g.id ");	

	while ( $row = mysql_fetch_array ( $rs ) ) {
	$node ['id'] = $row ['group_id'];
	$node ['parent'] = "#";
	$node ['children'] = true;
	$node ['type'] = "group";
	$node ['text'] = $row ['name'];

	array_push ( $r, $node );
	}
}

//$rs = mysql_query ( "select ug.group_id, g.name from users_groups as ug, groups as g where ug.group_id=g.id ");	
/*$node ['id'] = "0";
$node ['type'] = "root";
$node ['parent'] = "#";
$node ['children'] = "true";
$node ['text'] = "root";

array_push ( $r, $node );*/

/*while ( $row = mysql_fetch_array ( $rs ) ) {
	$node ['id'] = $row ['group_id'];
	$node ['parent'] = "#";
	$node ['children'] = true;
	$node ['type'] = "group";
	$node ['text'] = $row ['name'];

	array_push ( $r, $node );
}*/

/*$rs = mysql_query ( "select ug.group_id, ug.user_id, u.username from users_groups as ug, users as u where ug.user_id=u.id" );

while ( $row = mysql_fetch_array ( $rs ) ) {
	$node ['id'] = "u".$row ['user_id'];
	$node ['parent'] = $row ['group_id'];
	$node ['type'] = "user";
	$node ['text'] = $row ['username'];

	array_push ( $r, $node );
}*/

echo json_encode ( $r );
?>